<link rel="stylesheet" type="text/css" href="/public/static/css/index.css" />
<script src="/public/static/js/synchro.js"></script>
<{include file="tpl/filter.tpl"}>

<div>
   <input class="label-success pull-left" type="checkbox" id="all" />&nbsp;<span id="aa">全选</span> 
   <a href="javascript:;" id="batch_approve" name="batch_approve" class="btn" style="margin-right: 5px;margin-bottom: 5px;" data-url="?r=send/batchSetStatus">批准</a>
   <a href="javascript:;" id="batch_reject" name="batch_reject" class="btn" style="margin-right: 5px;margin-bottom: 5px;background-color: red;" data-url="?r=send/batchSetStatus">驳回</a>
</div>

<{include file="tpl/grid.tpl"}>
	
<script>
	$(function(){
		
		//批准
		$(document).on('click','a[name="approve"]',function(){
		//$('a[name="approve"]').click(function(){ 
			_this = $(this);
			layer.confirm('确定批准该申请？', {
				btn: ['确定','取消'] //按钮
				,skin: 'layui-layer-molv'
			}, function(){
				var id = _this.attr('data-id');
				setStatus(id,2,_this);
			}, function(){
				//console.log('取消');
            });
		});
		
		//驳回
		$(document).on('click','a[name="reject"]',function(){
		//$('a[name="reject"]').click(function(){
			_this = $(this);
			layer.confirm('确定驳回该申请？', {
				btn: ['确定','取消'] //按钮
				,skin: 'layui-layer-molv'
			}, function(){
				var id = _this.attr('data-id');
				setStatus(id,3,_this);
			}, function(){
				//console.log('取消');
            });
		});
		
		
		//批量批准
		$(document).on('click','a[name="batch_approve"]',function(){
			layer.confirm('确定批量批准申请？', {
				btn: ['确定','取消'] //按钮
				,skin: 'layui-layer-molv'
			}, function(){
				var arr = new Array();
                $("input[name='id']:checkbox[checked]").each(function(i){
                	arr[i] = $(this).val();
                });
                var ids = arr.join(",");
                //console.log(ids);
                if(ids == ""){
                	layer.msg('请先选择要批准的申请！', {icon: 2,time: 3000});
            	    return false;
                }
                
                var data_url = $("#batch_approve").attr('data-url');
                var g = $("input[name='g']").val();
                var surl = data_url+'&g='+g;
				batchSetStatus(ids,surl,2);
                
			}, function(){
				//console.log('取消');
            });
		});
		
		
		//批量驳回
		$(document).on('click','a[name="batch_reject"]',function(){
			layer.confirm('确定批量驳回申请？', {
				btn: ['确定','取消'] //按钮
				,skin: 'layui-layer-molv'
			}, function(){
				var arr = new Array();
                $("input[name='id']:checkbox[checked]").each(function(i){
                	arr[i] = $(this).val();
                });
                var ids = arr.join(",");
                //console.log(ids);
                if(ids == ""){
                	layer.msg('请先选择要驳回的申请！', {icon: 2,time: 3000});
            	    return false;
                }
                
                var data_url = $("#batch_reject").attr('data-url');
                var g = $("input[name='g']").val();
                var surl = data_url+'&g='+g;
				batchSetStatus(ids,surl,3);
                
			}, function(){
				//console.log('取消');
            });
		});
		  
	});
	
function batchSetStatus(ids,surl,status){		
	$.ajax({
    	type : 'POST',
    	url : surl,
		data : {'ids':ids,'status':status},
		dataType :'JSON',
		success : function(res){
			if(res.status == 1){
				var new_status_array = res.data;
				layer.alert(res.msg, {icon: 1, skin: 'layui-layer-molv'});
				$("input[name='id']:checkbox[checked]").each(function(i){
					//console.log(new_status_array[i]);
                	$(this).prop("checked", false);
                	$(this).prop("disabled", true);
                	var id = $(this).val();
                	var action = '<a href="javascript:;" name="xinfo" data-id="'+id+'"><font color="blue">审批信息</font></a>';
                	var new_status = new_status_array[i];
                	var html = "";
                	if(new_status == 2){
                		html = '<font color="blue">已批准</font>';
                	}else if(new_status == 3){
                		html = '<font color="red">已驳回</font>';
                	}
                	$(this).parent('td').parent('tr').find('.xstatus').html(html);
                	$(this).parent('td').parent('tr').find('.xaction').html(action);
                });
				
			}else{
			    layer.alert(res.msg, {icon: 2, skin: 'layui-layer-molv'});
			}
		}
	});
}
	
function setStatus(id,status,_this){
	var _this = _this;
	var g = $('input[name="g"]').val();
	var url = '?r=send/setStatus&g='+g;
	
	var html = "";
	var action = '<a href="javascript:;" name="xinfo" data-id="'+id+'"><font color="blue">审批信息</font></a>';

	$.ajax({
    	type : 'POST',
    	url : url,
		data : {'id':id,'status':status},
		dataType :'JSON',
		success : function(res){
			if(res.status == 1){
				layer.alert(res.msg, {icon: 1, skin: 'layui-layer-molv'});
				if(res.data == 2){
		            html = '<font color="blue">已批准</font>';
	            }else if(res.data == 3){
		            html = '<font color="red">已驳回</font>';
	            }
	            _this.parent('div').parent('td').parent('tr').find('input[name="id"]').prop("disabled", true);
				_this.parent('div').parent('td').parent('tr').find('.xstatus').html(html);
				_this.parent('div').html(action);
			}else{
			    layer.alert(res.msg, {icon: 2, skin: 'layui-layer-molv'});
			}
		}
	});
}


$(document).on('click','a[name="xinfo"]',function(){
	var id = $(this).attr('data-id');
	var g = $('input[name="g"]').val();
    $url = '?r=send/getApproval&g='+g+'&id='+id;
    layer.open({
    	skin: 'layui-layer-molv'
    	,type: 2
    	,area: ['400px', '600px']
    	,title:'审批信息'
    	,content: $url
    	,btn: ['确定']   			
    });
});
</script>